require("babel-polyfill");
//const assert = require("assert");
const chai = require("chai");
const expect = chai.expect;
const should = chai.should();
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);

import User from "../src/user";

let user;
beforeEach(function() {
  user = new User();
});

describe("User", function() {
  describe("create", function() {
    it("should return metadata on success", function() {
      this.timeout(15000);
      const res = user.create("foo", "bar");
      res.should.eventually.have.property("alias");
      res.should.eventually.have.property("pub");
      return res.should.be.fulfilled;
    });

    it("should fail when user already exists", async function() {
      this.timeout(15000);
      await user.create("foo", "bar");
      const res = user.create("foo", "bar");
      return res.should.be.rejected;
    });

    it("should fail when alias is missing", function() {
      const res = user.create("", "bar");
      return res.should.be.rejected;
    });

    it("should fail when password is missing", function() {
      const res = user.create("foo", "");
      return res.should.be.rejected;
    });
  });

  describe("auth", function() {
    beforeEach(async function() {
      this.timeout(15000);
      await user.create("foo", "bar");
    });

    it("should return metadata on success", function() {
      const res = user.auth("foo", "bar");
      res.should.eventually.have.property("alias");
      res.should.eventually.have.property("pub");
      return res.should.be.fulfilled;
    });

    it("should fail when password is incorrect", function() {
      const res = user.auth("foo", "bad");
      return res.should.be.rejected;
    });

    it("should fail when alias is missing", function() {
      const res = user.auth("", "bar");
      return res.should.be.rejected;
    });

    it("should fail when password is missing", function() {
      const res = user.auth("foo", "");
      return res.should.be.rejected;
    });
  });
});
